package com.peng.lambda;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

/**
 * Sample1
 * 函数定义的是行为，每次调用都会重新运行
 *
 * @author lupeng
 * Created on 2022-04-15
 */
public class Sample1 {

    private int age;

    public Sample1() {
    }

    public void add() {
        System.out.printf("add %s%n", age);
        age++;
    }

    public void start() {
        new Thread(() -> {
            while (true) {
                add();
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }

    public Supplier<Map<String, Integer>> supplier = () -> {
        Map<String, Integer> personAge = new HashMap<>();
        personAge.put(Integer.toString(age), age);
        return personAge;
    };

    public static void main(String[] args) {
        Sample1 sample1 = new Sample1();
        sample1.start();
        System.out.println(sample1.supplier.get());

        for (int i = 0; i < 100; i++) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            System.out.println(sample1.supplier.get());
        }
    }
}
